package com.huawei.petapp2.activity.widget;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.huawei.petapp2.R;
import com.huawei.petapp2.network.EShopClient;
import com.huawei.petapp2.network.core.ApiInterface;
import com.huawei.petapp2.network.core.ResponseEntity;
import com.huawei.petapp2.network.core.UiCallback;
import com.huawei.petapp2.network.event.UserEvent;
import com.huawei.petapp2.utils.StatusBarUtils;
import com.jaeger.library.StatusBarUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;

/**
 * Created by 吕氏春秋i 2020/4/30
 * Describe:
 */


public abstract class BaseActivity extends AppCompatActivity {
    public static Context context;
    protected Unbinder unbinder;



    @Override

    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(setLayoutId());
        unbinder = ButterKnife.bind(this);
        context = this;
        // 竖屏锁定
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //初始化视图
        //设置沉浸式
        setStatusBar();
        //初始化View

        try {
            initView();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //初始化数据
        try {
            initData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //初始化事件监听
        initEvent();




    }

    public static Context getContext() {
        return context;
    }

    protected abstract int setLayoutId();

    protected void setStatusBar() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary));
    }

    public abstract void initView() throws Exception;

    public abstract void initData() throws Exception;

    public abstract void initEvent();

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        View mView = getWindow().peekDecorView();
        if (mView != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            assert imm != null;
            imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
        }
    }


    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(UserEvent event) {
    }


    public void setStatusBar(View view) {
        if (view == null) {
            return;
        }
        ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, StatusBarUtils.getStatusBarHeight(context));
        view.setLayoutParams(params);
    }

    /**
     * @param s
     */
    public void showToast(@NonNull String s) {
        Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
    }

    public void showFileDialog() {
    }

    public void hideFileDialog() {
    }


    /**
     * 通过资源res获得view
     *
     * @param res
     * @return
     */
    public View getViewByRes(@LayoutRes int res) {
        return LayoutInflater.from(context).inflate(res, null);
    }

    /**
     * 获得TextView 的文本
     *
     * @param tv
     * @return
     */
    public String getTV(TextView tv) {
        return tv == null ? "" : tv.getText().toString().trim();
    }

    /**
     * 异步请求
     *
     * @param apiInterface
     * @return
     */
    protected Call enqueue(final ApiInterface apiInterface) {
        UiCallback uiCallback = new UiCallback() {
            @Override
            public void onBusinessResponse(boolean success, ResponseEntity responseEntity) {
                BaseActivity.this.onBusinessResponse(apiInterface.getPath(), success, responseEntity);
            }
        };
        return EShopClient.getInstance().enqueue(apiInterface, uiCallback, getClass().getSimpleName());
    }

    protected abstract void onBusinessResponse(String apiPath, boolean success, ResponseEntity rsp);
}
